from flask import session, current_app, g,jsonify
from functools import wraps
from werkzeug.routing import  BaseConverter
from .response_code import RET
#定义正则匹配器
class re(BaseConverter):
    def __init__(self, url_map,regex):
        super().__init__(url_map)
        self.regex=regex

#定义登陆装饰器,封装用户的登陆数据
def user_login_data(view_func):
    @wraps(view_func)
    def wrapper(*args,**kwargs):
        # 1.从session中取出用户的user_id
        user_id = session.get("id")

        # 2通过user_id取出用户对象
        if user_id:
            g.user_id=user_id
            return view_func(*args,**kwargs)
        else:
            return jsonify(errno=RET.SESSIONERR,errmsg="请先登录")

        
    return wrapper